//
//  SINASettingAlertPasswordViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/26.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINASettingAlertPasswordViewController.h"
#import "SINAHttpRequestUtils.h"
#import "SINALoginViewController.h"
#import "SINAControllerUtils.h"
#import "SINANavLoginViewController.h"
#import "JPUSHService.h"

@interface SINASettingAlertPasswordViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *isShowPasswordImageView;
@property (weak, nonatomic) IBOutlet UIButton *commitButton;
//显示密码
@property (weak, nonatomic) IBOutlet UIView *showPasswordView;
//旧密码
@property (weak, nonatomic) IBOutlet UITextField *prePasswordField;
//新密码
@property (weak, nonatomic) IBOutlet UITextField *toPasswordFields;
@property(nonatomic,assign) BOOL isShowPassword;

@end

@implementation SINASettingAlertPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigationItem];
    [self initViewFunction];
    // Do any additional setup after loading the view from its nib.
}

-(void)initViewFunction{
    //设置lable的圆角显示
    self.commitButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.commitButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.commitButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.commitButton.clipsToBounds = YES;
    
    [self.commitButton setBackgroundColor: [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f]];
    
    self.isShowPassword = YES;
    
    // 1. 创建一个点击事件，点击时触发labelClick方法
    UITapGestureRecognizer *showPasswordTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectShowPasswordFunction)];
    // 2. 将点击事件添加到label上
    [self.showPasswordView addGestureRecognizer:showPasswordTapGestureRecognizer];
    // 可以理解为设置label可被点击
    self.showPasswordView.userInteractionEnabled = YES;
    
    
}

-(void)configNavigationItem{
    self.navigationController.navigationBarHidden = NO;
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    self.navigationItem.title=@"修改密码";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//显示输入密码
-(void)selectShowPasswordFunction{
    if (self.isShowPassword) {
        self.isShowPassword=NO;
        self.prePasswordField.secureTextEntry = NO;
        self.toPasswordFields.secureTextEntry = NO;
        self.isShowPasswordImageView.image=[UIImage imageNamed:@"setting_select_icon"];
    } else {
        self.isShowPassword=YES;
        self.prePasswordField.secureTextEntry = YES;
        self.toPasswordFields.secureTextEntry = YES;
        
        self.isShowPasswordImageView.image=[UIImage imageNamed:@"setting_select_no_icon"];
    }
}
- (IBAction)checkPasswordFunction:(id)sender {
    
    [self.view endEditing:YES];
    //原密码
    NSString *prePassword=self.prePasswordField.text;
    //新密码
    NSString *currentPassword=self.toPasswordFields.text;
    if (prePassword==nil||prePassword.length<1) {
        WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入原始密码" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    if (currentPassword==nil||currentPassword.length<1) {
        WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输信新密码" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    [self commitAlertPasswordFunction];
}
-(void) commitAlertPasswordFunction{
    
    //原密码
    NSString *prePassword=self.prePasswordField.text;
    //新密码
    NSString *currentPassword=self.toPasswordFields.text;
    
    SINACommonUserHelper *helper =[SINACommonUserHelper share];
    SINAUserModel *userModel = [helper getUserModel];
    if (userModel!=nil&&userModel.userUid!=nil&&![userModel.userUid isEqualToString:@""]) {

        [self startLoading];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:userModel.userUid forKey:@"uuid"];
        [dict setObject:prePassword forKey:@"oldpass"];
        [dict setObject:currentPassword forKey:@"newpass"];
        
        NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
        [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
        WeakSelf;
        [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userAlertPasswordUrl] withParamters:dict2 success:^(id responseData) {
            NSString *code = responseData[@"code"];
            [weakSelf stopLoading];
            if ([code isEqualToString:@"1000"]) {
                //移除推送
                [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                } seq:101];
                //清除用户信息
                [[SINACommonUserHelper share] clearUserModel];
                [YPXToastView showToastViewWithText:@"修改密码成功 请重新登录" andDuration:2 andCorner:5 andParentView:self.view];
        
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
                    SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
                    [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];;
                });
                
            }else{
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
        } failure:^(NSError *error, NSInteger errCode) {
            DLog(@"加载 失败  %@ ",error.debugDescription);
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"登录验证失败" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }];
    }else{
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常请重新进入" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
